import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _forEach from "lodash/forEach";
import _uniqWith from "lodash/uniqWith";
import _filter from "lodash/filter";
import _set from "lodash/set";
import _map from "lodash/map";
var _excluded = ["hiddenColumns"];

/**
 * HiddenColumnsEditModal.js
 * 隐藏域编辑
 * @date 2018/10/26
 */
import React from 'react';
import { Modal, Input, Button, Popconfirm } from 'antd';
import uuid from 'uuid/v4';
import EditTable from 'components/EditTable';
import { hiddenColumnKey, hiddenColumnPrefix, hiddenColumnSep } from 'components/DynamicComponent/DynamicTable/utils';
import { getEditTableData } from 'utils/utils';
import intl from 'utils/intl';
import notification from 'utils/notification';
import { attributeNameProp, attributeTypeProp, attributeValueProp } from "../../config";
import DataType from "../../DataType";
var editRecordField = '_status';
var editCreate = 'create';
var editEditing = 'update';
var modalBodyStyle = {
  minHeight: 300,
  overflowY: 'auto'
};

var HiddenColumnsEditModal = /*#__PURE__*/function (_React$Component) {
  _inherits(HiddenColumnsEditModal, _React$Component);

  var _super = _createSuper(HiddenColumnsEditModal);

  function HiddenColumnsEditModal(props) {
    var _this;

    _classCallCheck(this, HiddenColumnsEditModal);

    _this = _super.call(this, props);
    _this.state = {// hiddenColumns: [], // 用于比较 和 props 的区别
      // hiddenColumnsDataSource: [], // 编辑的数据源
    };
    _this.handleHiddenColumnRemove = _this.handleHiddenColumnRemove.bind(_assertThisInitialized(_this));
    _this.handleHiddenColumnAdd = _this.handleHiddenColumnAdd.bind(_assertThisInitialized(_this));
    _this.handleSaveHiddenColumns = _this.handleSaveHiddenColumns.bind(_assertThisInitialized(_this));
    return _this;
  }

  _createClass(HiddenColumnsEditModal, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          hiddenColumns = _this$props.hiddenColumns,
          modalProps = _objectWithoutProperties(_this$props, _excluded);

      var _this$state$hiddenCol = this.state.hiddenColumnsDataSource,
          hiddenColumnsDataSource = _this$state$hiddenCol === void 0 ? [] : _this$state$hiddenCol;
      return /*#__PURE__*/React.createElement(Modal, Object.assign({
        title: "\u9690\u85CF\u57DF",
        bodyStyle: modalBodyStyle
      }, modalProps, {
        onOk: this.handleSaveHiddenColumns
      }), /*#__PURE__*/React.createElement("div", {
        style: {
          marginBottom: 10
        }
      }, /*#__PURE__*/React.createElement(Button, {
        icon: "plus",
        onClick: this.handleHiddenColumnAdd
      })), /*#__PURE__*/React.createElement(EditTable, {
        rowKey: "columnKey",
        columns: this.getColumns(),
        dataSource: hiddenColumnsDataSource,
        bordered: true,
        pagination: false
      }));
    }
  }, {
    key: "getColumns",
    value: function getColumns() {
      var _this2 = this;

      if (!this.columns) {
        this.columns = [{
          key: 'action',
          title: intl.get('cpaas.common.button.action').d('操作'),
          width: 80,
          render: function render(item, record) {
            return /*#__PURE__*/React.createElement(Popconfirm, {
              title: intl.get('cpaas.common.message.confirm.remove').d('确定删除选中数据？'),
              onConfirm: function onConfirm() {
                return _this2.handleHiddenColumnRemove(record);
              }
            }, /*#__PURE__*/React.createElement("a", null, intl.get('cpaas.common.button.delete').d('删除')));
          }
        }, {
          title: '标题',
          dataIndex: 'columnLabel',
          render: function render(item, record) {
            var getFieldDecorator = record.$form.getFieldDecorator;
            return getFieldDecorator('columnLabel', {
              initialValue: item,
              rules: [{
                pattern: new RegExp("[^".concat(hiddenColumnSep, "]")),
                message: "\u4E0D\u80FD\u5305\u542B: ".concat(hiddenColumnSep)
              }]
            })( /*#__PURE__*/React.createElement(Input, null));
          }
        }, {
          title: '字段名',
          dataIndex: 'columnName',
          width: 200,
          render: function render(item, record) {
            var getFieldDecorator = record.$form.getFieldDecorator;
            return getFieldDecorator('columnName', {
              initialValue: item,
              rules: [{
                pattern: new RegExp("[^".concat(hiddenColumnSep, "]")),
                message: "\u4E0D\u80FD\u5305\u542B: ".concat(hiddenColumnSep)
              }]
            })( /*#__PURE__*/React.createElement(Input, null));
          }
        }];
      }

      return this.columns;
    }
  }, {
    key: "handleHiddenColumnRemove",
    value: function handleHiddenColumnRemove(removeRecord) {
      var _this$state$hiddenCol2 = this.state.hiddenColumnsDataSource,
          hiddenColumnsDataSource = _this$state$hiddenCol2 === void 0 ? [] : _this$state$hiddenCol2;
      this.setState({
        hiddenColumnsDataSource: _filter(hiddenColumnsDataSource, function (record) {
          return removeRecord !== record;
        })
      });
    }
  }, {
    key: "handleHiddenColumnAdd",
    value: function handleHiddenColumnAdd() {
      var _this$state$hiddenCol3 = this.state.hiddenColumnsDataSource,
          hiddenColumnsDataSource = _this$state$hiddenCol3 === void 0 ? [] : _this$state$hiddenCol3;
      this.setState({
        hiddenColumnsDataSource: [].concat(_toConsumableArray(hiddenColumnsDataSource), [_defineProperty({
          columnKey: uuid()
        }, editRecordField, editCreate)])
      });
    }
  }, {
    key: "handleSaveHiddenColumns",
    value: function handleSaveHiddenColumns() {
      var onOk = this.props.onOk;
      var _this$state$hiddenCol4 = this.state.hiddenColumnsDataSource,
          hiddenColumnsDataSource = _this$state$hiddenCol4 === void 0 ? [] : _this$state$hiddenCol4;
      var saveDataSource = getEditTableData(hiddenColumnsDataSource, []);
      var errMessage;

      var uniqDataSource = _uniqWith(saveDataSource, function (r1, r2) {
        return r1.columnName === r2.columnName;
      });

      if (uniqDataSource.length !== saveDataSource.length) {
        errMessage = '隐藏域字段名 必须唯一';
        return false;
      }

      if (errMessage) {
        notification.error({
          message: errMessage
        });
        return;
      }

      var hiddenColumns = [];

      _forEach(saveDataSource, function (hiddenColumn) {
        var _hiddenColumns$push;

        hiddenColumns.push((_hiddenColumns$push = {}, _defineProperty(_hiddenColumns$push, attributeNameProp, "".concat(hiddenColumnPrefix, "[").concat(hiddenColumn.columnKey, "]")), _defineProperty(_hiddenColumns$push, attributeValueProp, "".concat(hiddenColumn.columnName).concat(hiddenColumnSep).concat(hiddenColumn.columnLabel)), _defineProperty(_hiddenColumns$push, attributeTypeProp, DataType.String), _hiddenColumns$push));
      });

      onOk(hiddenColumns);
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(nextProps, prevState) {
      var hiddenColumns = nextProps.hiddenColumns;
      var prevHiddenColumns = prevState.hiddenColumns;

      if (hiddenColumns !== prevHiddenColumns) {
        return {
          hiddenColumns: hiddenColumns,
          hiddenColumnsDataSource: _map(hiddenColumns, function (hiddenColumn) {
            var c = {};

            _set(c, hiddenColumn[attributeNameProp], hiddenColumn);

            var columnKey = Object.keys(c[hiddenColumnKey])[0];

            var _hiddenColumn$attribu = hiddenColumn[attributeValueProp].split(hiddenColumnSep),
                _hiddenColumn$attribu2 = _slicedToArray(_hiddenColumn$attribu, 2),
                columnName = _hiddenColumn$attribu2[0],
                columnLabel = _hiddenColumn$attribu2[1];

            return _defineProperty({
              columnKey: columnKey,
              columnLabel: columnLabel,
              columnName: columnName
            }, editRecordField, editEditing);
          })
        };
      }

      return null;
    }
  }]);

  return HiddenColumnsEditModal;
}(React.Component);

export { HiddenColumnsEditModal as default };